#include "import_face_data_db.hpp"

rockx_handle_t face_det_handle;
rockx_handle_t face_landmark_handle;
rockx_handle_t face_rec_handle;

#if 1
static int load_face_feature_to_sqlite(char *name, rockx_image_t *img, rockx_face_feature_t *out_feature)
{
  rockx_ret_t ret;
  rockx_object_array_t face_array;
  memset(&face_array, 0, sizeof(rockx_object_array_t));
  ret = rockx_face_detect(face_det_handle, img, &face_array, NULL);
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_face_detect error ..." << __FILE__ << __func__ << __LINE__ << endl;
    return -1;
  }

  if (face_array.count == 0)
  {
    cout << "Don't detect face ..." << endl;
    return -1;
  }

  rockx_object_t *max_face = NULL;
  int i;
  for (i = 0; i < face_array.count; i++)
  {
    int left = face_array.object[i].box.left;
    int top = face_array.object[i].box.top;
    int right = face_array.object[i].box.right;
    int bottom = face_array.object[i].box.bottom;
    int width = right - left;
    int height = bottom - top;
    cout << "face_frame(width, height):" << width << " " << height << endl;
    cout << "entering face detect score:" << face_array.object[i].score << endl;

    rockx_object_t *cur_face = &(face_array.object[i]);
    if (max_face == NULL)
    {
      max_face = cur_face;
      continue;
    }
    int cur_face_area = width * height;
    int max_face_area = (max_face->box.right - max_face->box.left) * (max_face->box.bottom - max_face->box.top);
    if (max_face_area < cur_face_area)
    {
      max_face = cur_face;
    }
  }
  printf("get_max_face %d\n", i);

  rockx_image_t align_img;
  memset(&align_img, 0, sizeof(rockx_image_t));
  ret = rockx_face_align(face_landmark_handle, img, &(max_face->box), NULL, &align_img);
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_face_align error ..." << __FILE__ << __func__ << __LINE__ << endl;
    return -1;
  }

  ret = rockx_face_recognize(face_rec_handle, &align_img, out_feature);
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_face_recognize error ..." << __FILE__ << __func__ << __LINE__ << endl;
    rockx_image_release(&align_img);
    return -1;
  }

  insert_face_data_to_databases(name, FEATURESIZE, out_feature->feature);
  rockx_image_release(&align_img);
  return 0;
}

int main(int argc, char **argv)
{
  if (argc != 3)
  {
    cout << "Usage: exec name file" << endl;
    return -1;
  }

  char *name = argv[1];
  char *file = argv[2];
  rockx_ret_t ret;
  rockx_config_t *config = rockx_create_config();
  ret = rockx_add_config(config, ROCKX_CONFIG_DATA_PATH, "/demo/src/rockx_data");
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_add_config error ..." << __FILE__ << __func__ << __LINE__ << endl;
    return -1;
  }
  ret = rockx_create(&face_det_handle, ROCKX_MODULE_FACE_DETECTION, config, 0);
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_create error ..." << __FILE__ << __func__ << __LINE__ << endl;
    return -1;
  }
  ret = rockx_create(&face_landmark_handle, ROCKX_MODULE_FACE_LANDMARK_5, config, 0);
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_create error ..." << __FILE__ << __func__ << __LINE__ << endl;
    return -1;
  }
  ret = rockx_create(&face_rec_handle, ROCKX_MODULE_FACE_RECOGNIZE, config, 0);
  if (ret != ROCKX_RET_SUCCESS)
  {
    cout << "rockx_create error ..." << __FILE__ << __func__ << __LINE__ << endl;
    return -1;
  }

  rockx_image_t img;
  memset(&img, 0, sizeof(rockx_image_t));
  rockx_image_read(file, &img, 1);
  rockx_face_feature_t out_feature;
  memset(&out_feature, 0, sizeof(rockx_face_feature_t));
  load_face_feature_to_sqlite(name, &img, &out_feature);
  return 0;
}
#endif
